/**
 *
 * 字符串操作类
 *
 */
class StringUtils extends BaseClass {
    /**
     * 构造函数
     */
    public constructor() {
        super();
    }
	
	/**
	 * 字符串头尾去除空格
	 */
    public static trim(str: string): string {
        return str.trim();
    }
    /**
     * 是否包含中文
     */
    public static isChinese(str: string): boolean {
        // //从开头到结尾^ $,出现一次+
        var reg = ".*[\u4e00-\u9fa5]+.*$";
        if(!str.match(reg)) {
            return false;
        }
        return true;
    }
	
    /**
     * 获取字符串char数组
     */
    public static getChars(str: string): string[] {
        var chars: string[] = str.split("");
        return chars;
    }
    
    /**
     * 计算字符串长度
     * 中文占2个字符
     */
    public static getStringLength(str: string): number {
        var chars: string[] = this.getChars(str);
        var length = 0;

        for(var i = 0;i < chars.length;i++) {
            if(StringUtils.isChinese(chars[i])) {
                length += 2;
            } else {
                length += 1;
            }
        }
        return length;
    }
    
    /**
     * 删除字符串第i个位置,length长度
     */
    public static deleteCharAt(str: string,i: number,length: number = 1): string {
        return str.substring(0,i) + str.substring(i + length,str.length);
    }
    
    /**
     * 生成随机的中文字符
     *  汉字范围： [\u4e00-\u9fa5]
     */
    public static randomGBChar(): string {
        return String.fromCharCode(MathUtils.randomInt(0x4e00,0x9fa5));
    }
    
    /**
     * 生成指定长度的随机中文字符串
     * @param length
     * @returns {string}
     */
    public static randomGBString(length: number = 1): string {
        var str: string = "";
        for(var i: number = 0;i < length;i++) {
            str += this.randomGBChar();
        }

        return str;
    }

    /**
     * 生成指定长度的随机英文大小写字符以及数字字符构成的字符串
     * @param length
     * @returns {string}
     */
    public static randomString(length: number = 1): string {
        var lib: string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var str: string = "";
        for(var i: number = 0;i < length;i++) {
            str += lib.charAt(Math.floor(Math.random() * lib.length))
        }
        return str;
    }
    
    
    /**
     * test case
     */
    public static test(): void {
        var aaa: string = "  jht 江";
        Log.d("test:" + this.isChinese(aaa));

        aaa = StringUtils.trim(aaa);
        var c: string[] = this.getChars(aaa);
        for(var i = 0;i < c.length;i++) {
            Log.trace("string:" + c[i]);
        }
        Log.d("length:" + this.getStringLength(aaa));
    }


}
